package com.songcha.module_main.ui.view

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.PorterDuff
import android.graphics.PorterDuffXfermode
import android.graphics.Rect
import android.media.MediaPlayer
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.AnimationSet
import android.view.animation.AnimationUtils
import android.view.animation.RotateAnimation
import android.view.animation.ScaleAnimation
import android.view.animation.TranslateAnimation
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import com.songcha.library_common.helper.MediaPlayerHelper
import com.songcha.library_common.helper.ViewHelper
import com.songcha.library_common.util.AssetsUtil
import com.songcha.library_common.util.LogUtil
import com.songcha.library_common.util.ToastUtil
import com.songcha.library_common.util.dp2px
/*import com.songcha.library_common.util.BitmapUtil
import com.songcha.library_common.util.LogUtil
import com.songcha.library_common.util.ToastUtil
import com.songcha.library_common.util.dp2px
import com.songcha.library_common.util.sp2px*/
import com.songcha.module_main.R

class WoodenFishView
@JvmOverloads
constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0,
    defRes: Int = 0
) : FrameLayout(context,attrs,defStyle,defRes) {
    private lateinit var mIvWoodenFish:ImageView
    private lateinit var mIvStick:ImageView
    private var mWoodenFishAnimation:Animation? = null
    private var mStickAnimation:Animation? = null
    //private var mGongDeAnimation:Animation? = null
    private var mGongDeAnimationSet:AnimationSet? = null
    private val mWoodenFishWidth = 205.dp2px()
    private val mWoodenFishHeight = 179.dp2px()
    private val mStickWidth = 194.dp2px()
    private val mStickHeight = 95.dp2px()
    private var mWoodenFishMarginTop = 70.dp2px()
    private var mWoodenFishHeightRatio = 0.8
    private lateinit var mTvGongDe:TextView
    private val mTvGongDeTextSize = 14f
    private val mTvGongDeTextColor = Color.parseColor("#FFE1CC")
    private val mTvGongDeMarginTop = 200.dp2px()
    private lateinit var mTvContinuousStrike:TextView
    private val mTvContinuousStrikeTextSize = 31f
    private val mTvContinuousStrikeTextColor = Color.parseColor("#FFE1CC")
    private val mTvContinuousStrikeMarginTop = 80.dp2px()
    private val mTvContinuousStrikeMarginRight = 15.dp2px()
    private var mContinuousStrikeCount = 0
    private var mLastStrikeTime = 0L
    private var mStrikeWoodenFishListener:IStrikeWoodenFishListener? = null
    var isStrikeVoiceEnabled = true
    private var mIsContinueStrike = false

    companion object{
        const val CONTINUOUS_STRIKE_TIME_INTERVAL = 800
    }

    interface IStrikeWoodenFishListener{
        fun onStrikeWoodenFishStar(continuousStrikeCount:Int,isContinueStrike:Boolean)
        fun onStrikeWoodenFishEnd(continuousStrikeCount:Int,isContinueStrike:Boolean)
    }

    init {
        initView(context, attrs)
    }

    private fun initView(context: Context, attrs: AttributeSet?){
        initWoodenFish()

        /*setOnClickListener {
            strikeWoodenFish()
        }*/
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)

    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        super.onLayout(changed, l, t, r, b)
        //initWoodenFish()
    }

    fun setStrikeWoodenFishListener(listener:IStrikeWoodenFishListener){
        mStrikeWoodenFishListener = listener
    }

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        return true
    }

    /*override fun onTouchEvent(event: MotionEvent): Boolean {
        when(event.action){
            MotionEvent.ACTION_DOWN->{

            }
            MotionEvent.ACTION_UP->{
                strikeWoodenFish()
            }
        }
        return true
    }*/

     fun strikeWoodenFish(){
        if(mWoodenFishAnimation == null){
            mWoodenFishAnimation = AnimationUtils.loadAnimation(context,R.anim.main_anim_muyu_scale)
            mWoodenFishAnimation!!.setAnimationListener(object: Animation.AnimationListener {
                override fun onAnimationStart(animation: Animation?) {

                }

                override fun onAnimationEnd(animation: Animation?) {
                    if(isStrikeVoiceEnabled)
                        MediaPlayerHelper.playMp3(context,R.raw.main_mp3_strike_muyu)
                }

                override fun onAnimationRepeat(animation: Animation?) {

                }

            })
        }

        mIvWoodenFish.startAnimation(mWoodenFishAnimation)


        if(mStickAnimation == null)
            mStickAnimation = AnimationUtils.loadAnimation(context,R.anim.main_anim_stick_rotate)
        mIvStick.startAnimation(mStickAnimation)

        /*if(mGongDeAnimation == null){
            mGongDeAnimation = TranslateAnimation(0f,0f,0f, mTvGongDeMarginTop * -0.5f)
            mGongDeAnimation!!.duration = 500
            mGongDeAnimation!!.setAnimationListener(object: Animation.AnimationListener {
                override fun onAnimationStart(animation: Animation?) {
                    mTvGongDe.visibility = View.VISIBLE
                }

                override fun onAnimationEnd(animation: Animation?) {
                    mTvGongDe.visibility = View.GONE
                    mTvContinuousStrike.visibility = View.GONE
                    mStrikeWoodenFishListener?.onStrikeWoodenFishEnd(mContinuousStrikeCount,mIsContinueStrike)
                }

                override fun onAnimationRepeat(animation: Animation?) {

                }

            })
        }*/
         if(mGongDeAnimationSet == null){
             mGongDeAnimationSet = AnimationSet(true)
             val gongDeTranslateAnimation = TranslateAnimation(0f,0f,0f, mTvGongDeMarginTop * -0.3f)
             gongDeTranslateAnimation!!.duration = 500

             /*val gongDeScaleAnimation = ScaleAnimation(1f,1f,0.2f, 0.2f)
             gongDeScaleAnimation!!.duration = 500*/

             mGongDeAnimationSet!!.addAnimation(gongDeTranslateAnimation)
             //mGongDeAnimationSet!!.addAnimation(gongDeScaleAnimation)

             mGongDeAnimationSet!!.setAnimationListener(object: Animation.AnimationListener {
                 override fun onAnimationStart(animation: Animation?) {
                     mTvGongDe.visibility = View.VISIBLE
                 }

                 override fun onAnimationEnd(animation: Animation?) {
                     mTvGongDe.visibility = View.GONE
                     mTvContinuousStrike.visibility = View.GONE
                     mStrikeWoodenFishListener?.onStrikeWoodenFishEnd(mContinuousStrikeCount,mIsContinueStrike)
                 }

                 override fun onAnimationRepeat(animation: Animation?) {

                 }

             })
         }

        mTvGongDe.startAnimation(mGongDeAnimationSet)

        if(System.currentTimeMillis() - mLastStrikeTime < CONTINUOUS_STRIKE_TIME_INTERVAL){
            mContinuousStrikeCount++
            mIsContinueStrike = true
        }else{
            mContinuousStrikeCount = 1
            mIsContinueStrike = false
        }

        if(mContinuousStrikeCount >= 3)
            mTvContinuousStrike.visibility = View.VISIBLE

        mTvContinuousStrike.text = "连敲x${mContinuousStrikeCount}"

        //if(isStrikeVoiceEnabled)
            //MediaPlayerHelper.playMp3(context,R.raw.main_mp3_strike_muyu)

        mLastStrikeTime = System.currentTimeMillis()

        mStrikeWoodenFishListener?.onStrikeWoodenFishStar(mContinuousStrikeCount,mIsContinueStrike)

    }

    private fun initWoodenFish() {

        removeAllViews()

        mIvWoodenFish = ImageView(context)
        val ivWoodenFishLp = LayoutParams(mWoodenFishWidth,mWoodenFishHeight)
        mIvWoodenFish.setImageResource(R.mipmap.main_bg_muyu)
        ivWoodenFishLp.gravity = Gravity.CENTER
        ivWoodenFishLp.topMargin = mWoodenFishMarginTop
        mIvWoodenFish.scaleType = ImageView.ScaleType.CENTER_CROP
        mIvWoodenFish.layoutParams = ivWoodenFishLp
        addView(mIvWoodenFish)

        mIvStick = ImageView(context)
        val ivStickLp = LayoutParams(mStickWidth,mStickHeight)
        ivStickLp.bottomMargin = (mWoodenFishHeight * mWoodenFishHeightRatio).toInt() - mWoodenFishMarginTop
        ivStickLp.rightMargin = -15.dp2px()
        mIvStick.setImageResource(R.mipmap.main_bg_stick)
        ivStickLp.gravity = Gravity.CENTER_VERTICAL or Gravity.END
        mIvStick.scaleType = ImageView.ScaleType.CENTER_CROP
        mIvStick.layoutParams = ivStickLp
        addView(mIvStick)

        mTvGongDe = TextView(context)
        val tvGongDeLp = LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)
        tvGongDeLp.topMargin = mTvGongDeMarginTop
        tvGongDeLp.gravity = Gravity.CENTER_HORIZONTAL
        mTvGongDe.textSize = mTvGongDeTextSize
        mTvGongDe.setTextColor(mTvGongDeTextColor)
        mTvGongDe.layoutParams = tvGongDeLp
        mTvGongDe.text = "功德+1\n烦恼-1"
        mTvGongDe.visibility = View.GONE
        addView(mTvGongDe)

        mTvContinuousStrike = TextView(context)
        val tvContinuousStrikeLp = LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)
        tvContinuousStrikeLp.topMargin = mTvContinuousStrikeMarginTop
        //tvContinuousStrikeLp.rightMargin = mTvContinuousStrikeMarginRight
        tvContinuousStrikeLp.gravity = Gravity.CENTER_HORIZONTAL
        mTvContinuousStrike.textSize = mTvContinuousStrikeTextSize
        mTvContinuousStrike.setTextColor(mTvContinuousStrikeTextColor)
        mTvContinuousStrike.layoutParams = tvContinuousStrikeLp
        mTvContinuousStrike.text = "连敲x1"
        mTvContinuousStrike.visibility = View.GONE
        addView(mTvContinuousStrike)

    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        MediaPlayerHelper.destroy()
    }

}